﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace Project
{
    public partial class Frm_Update : Form
    {
        /// <summary>
        /// 0：下载文件夹名
        /// 1：申报系统exe路径
        /// 2：退税金钥匙进程名
        /// 3：升级程序进程名
        /// </summary>
        string[] param;
        string path = AppDomain.CurrentDomain.BaseDirectory + "../sc_dl_2.0.1.160314.exe";
        public Frm_Update()
        {
            InitializeComponent();
        }

        private void Form3_Shown(object sender, EventArgs e)
        {
            //this.Text = "a";
            //System.Threading.Thread.Sleep(1000);
            //this.Text = "b";
            //System.Threading.Thread.Sleep(1000);
            //MessageBox.Show("升级完成");
            //this.Visible = false;
            //Process debug = new Process();   //创建新的进程                     
            //debug.StartInfo.FileName = path;
            //debug.StartInfo.UseShellExecute = false;
            //debug.StartInfo.CreateNoWindow = true;
            //if (debug.Start())
            //{
            //    this.Close();
            //}

        }

        private void button1_Click(object sender, EventArgs e)
        {
            param = new string[] { Application.StartupPath + "\\update160313", "Project.exe", "", "gdkey_update.vshost.exe" };
            StartProcess(Application.StartupPath + "\\update\\gdkey_update.exe", param);
        }

        /// <summary>
        ///  更新引导程序及flash.
        /// </summary>
        public bool UpdateYDCX(string path)
        {
            //解压升级文件.
            //UnZIP.UnZip(path, Application.StartupPath);

            if (Directory.Exists(path))
            {
                string[] files = Directory.GetFiles(path);
                if (files.Length <= 0)
                {
                    return false;
                }

                foreach (string item in files)
                {
                    File.Copy(item, Application.StartupPath + "\\../" + new FileInfo(item).Name, true);
                    File.Delete(item);
                }

                ////存在升级文件时复制到上一级目录.
                //if (File.Exists(Application.StartupPath + "\\" + param[1]))
                //{
                //    File.Copy(Application.StartupPath + "\\" + param[1], Application.StartupPath + "\\../" + param[5], true);
                //    File.Delete(Application.StartupPath + "\\" + param[1]);
                //}
                Directory.Delete(path);
                return true;
            }

            return false;
        }

        /// <summary>
        /// 启动指定路径的exe应用程序.
        /// </summary>
        /// <param name="path">指定exe所在绝对路径.</param>
        /// <param name="param">传入参数.</param>
        /// <returns></returns>
        public static bool StartProcess(string path, object[] param)
        {
            try
            {
                Process debug = new Process();   //创建新的进程                     
                debug.StartInfo.FileName = path;
                debug.StartInfo.UseShellExecute = false;
                debug.StartInfo.CreateNoWindow = true;
                string ArgumentsStr = string.Empty;
                if (param != null && param.Length > 0)
                {
                    ArgumentsStr = "";
                    foreach (object item in param)
                    {
                        ArgumentsStr += (item.ToString() + ";");
                    }
                    ArgumentsStr = ArgumentsStr.TrimEnd(';');
                }
                if (ArgumentsStr != string.Empty && ArgumentsStr != "") debug.StartInfo.Arguments = ArgumentsStr;
                if (debug.Start())
                {
                    return true;
                }
                return false;
            }
            catch (System.Exception)
            {
                return false;
            }
        }
    }
}
